﻿<script type="text/javascript" src="json2.min.js"></script>
<script type="text/javascript">
Date.prototype.format = function(format)
{
	var o = {
	"M+" : this.getMonth()+1, //month
	"d+" : this.getDate(),    //day
	"h+" : this.getHours(),   //hour
 	"m+" : this.getMinutes(), //minute
	"s+" : this.getSeconds(), //second
 	"q+" : Math.floor((this.getMonth()+3)/3),  //quarter
 	"S" : this.getMilliseconds() //millisecond
	}

	if(/(y+)/.test(format)) 
	{
		format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
	}
	
 	for(var k in o)
	{
		if(new RegExp("("+ k +")").test(format))
		{
 			format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
		}
	}
	return format;
}

var d = new Date();
document.write('日期对象为：');
document.write(d.format('yyyy-MM-dd'));

//只能是数字
String.prototype.isDigit = function()
{
    var reg = /^\d+$/g;
    return reg.test(this);
}

//只能是数字和字母
String.prototype.isAlphaDigit = function()
{
    var reg = /^[a-zA-Z0-9]+$/g;
    return reg.test(this);
}

//只能是数字、字母与下划线
String.prototype.isAlpha = function()
{
    var reg = /^\w+$/g;
    return reg.test(this);
}

//alert("asdfadf23423 4_adf".isAlpha());

//是否是格式规整的json，必须是严格格式的
function isJson(v) {
	try{
		JSON.parse(v);
		return true;
	}
	catch(e){
		return false;
	}
	return false;
	
	v = v.replace(/\\["\\\/bfnrtu]/g, '@');
	v = v.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
	v = v.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
	return (/^[\],:{}\s]*$/.test(v))
}
//alert(isJson("{\"a\":1}"));
//alert(isJson("true"));//
//alert(isJson("1"));//
//alert(isJson("{}}"));//false

String.prototype.trim = function(){
	return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}

String.prototype.startsWith = function(str){
	return (this.match("^"+str)==str);
}

String.prototype.endsWith = function(str){
	return (this.match(str+"$")==str);
}

//alert(" axxx".trim().startsWith("a"));
var v = "{";
if(!v.startsWith("{") || !v.endsWith("}"))
	alert(0);
</script>
